package cn.whale.pay.config;

import cn.whale.pay.mq.callback.PayMsgConfirmCallback;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static cn.whale.basic.constants.MQConstants.*;

//rabbitMQ的配置
@Configuration
public class MQConfig {

    @Autowired
    private PayMsgConfirmCallback payMsgConfirmCallback;

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        rabbitTemplate.setConfirmCallback(payMsgConfirmCallback);
        rabbitTemplate.setReturnCallback(payMsgConfirmCallback);
        rabbitTemplate.setMandatory(true);
        return rabbitTemplate;
    }

    //定义交换机
    @Bean
    public Exchange exchangeOrder(){
        return ExchangeBuilder.topicExchange(EXCHNAGE_ORDER_KILL).durable(true).build();
    }



    //该队列接收死信交换机转发过来的消息
    @Bean(QUEUE_PAY_NOTEFIY)
    public Queue queuePayNotify(){
        return new Queue(QUEUE_PAY_NOTEFIY,true);
    }

    @Bean
    public Binding queueOrderBinding(){
        return BindingBuilder.bind(queuePayNotify()).to(exchangeOrder()).with(ROUTINGKEY_QUEUE_PAY_NOTIFY).noargs();
    }
}